<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件(分为头部和身体) -->
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="组织结构" name="first" class="tab-pane">
              <!-- class="tab-pane"设置左右padding, 把内容往中间挤压 -->
              <!-- 使用 Layout 布局绘制头部区域
                最外层一行, 2列
                  el-row: type布局模式
                          justify两端对齐
                          align交叉轴排列方式(垂直方向居中)
               -->
              <el-row
                type="flex"
                justify="space-between"
                align="middle"
                class="department-header"
              >
                <!-- 第一列20份 -->
                <el-col :span="20">
                  <i class="el-icon-s-home" />
                  <span class="company">江苏传智播客教育科技股份有限公司</span>
                </el-col>
                <!-- 第二列4份 -->
                <el-col :span="4">
                  <!-- 在容器范围里, 一行再分2列 -->
                  <el-row type="flex" justify="end">
                    <el-col>负责人</el-col>
                    <el-col>
                      <!-- 这个之前在头部导航右侧下拉菜单讲过 -->
                      <el-dropdown>
                        <!-- 下拉菜单文字 -->
                        <span class="el-dropdown-link">
                          操作<i class="el-icon-arrow-down el-icon--right" />
                        </span>
                        <!-- 下拉项 -->
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="add()">添加子部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
              <!-- 树状结构 -->
              <el-tree
                class="departments-tree"
                :data="treeData"
                :props="defaultProps"
                :default-expand-all="true"
              >
                <!-- 默认插槽  并且使用内部的数据进行自定义模板的渲染
                  scope.data 所以直接key解构赋值变量也是data(对应数据对象)
                 -->
                <template #default="{ data }">
                  <el-row
                    type="flex"
                    justify="space-between"
                    align="middle"
                    style="height: 40px; width: 100%"
                  >
                    <!-- 部门名字 -->
                    <el-col :span="20">
                      <span>{{ data.name }}</span>
                    </el-col>
                    <el-col :span="4">
                      <el-row type="flex" justify="end">
                        <!-- 两个内容 -->
                        <el-col>{{ data.manager }}</el-col>
                        <el-col>
                          <!-- 下拉菜单 element -->
                          <el-dropdown>
                            <span> 操作<i class="el-icon-arrow-down" /> </span>
                            <!-- 下拉菜单 -->
                            <el-dropdown-menu slot="dropdown">
                              <el-dropdown-item
                                @click.native="add(data)"
                              >添加子部门</el-dropdown-item>
                              <el-dropdown-item
                                @click.native="edit(data)"
                              >编辑部门</el-dropdown-item>
                              <el-dropdown-item
                                v-if="data && !data.children"
                                @click.native="del(data)"
                              >删除部门</el-dropdown-item>
                            </el-dropdown-menu>
                          </el-dropdown>
                        </el-col>
                      </el-row>
                    </el-col></el-row></template>
              </el-tree>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-card>
    </div>
    <departDialog
      ref="dialog"
      :dialog-visible.sync="showDialog"
      :parent-id="parentId"
      :is-edit="isEdit"
      :origin-list="originList"
      @resetEv="resetFn"
    />
  </div>
</template>

<script>
import { departmentsListAPI, delDepartmentAPI } from '@/api'
import { transTree } from '@/utils'
import departDialog from './component/departDialog.vue'
export default {
  name: 'Departments',
  components: {
    departDialog
  },
  data() {
    return {
      activeName: 'first',
      treeData: [],
      // 定义结构显示
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 控制弹窗
      parentId: '', // 上级ID
      isEdit: false, // 控制弹窗 是否为编辑
      originList: [] // 映射的数组,传子组件,编号非重验证
    }
  },
  created() {
    this.getDepartmentsListFn()
  },
  methods: {
    async getDepartmentsListFn() {
      const res = await departmentsListAPI()
      const {
        data: { depts }
      } = res
      this.originList = depts.map(obj => {
        return {
          code: obj.code,
          id: obj.id,
          name: obj.name,
          pid: obj.pid
        }
      })
      this.treeData = depts
      const arr = transTree(this.treeData, '')
      this.treeData = arr
    },
    // 头部卡片的点击事件
    handleClick(tab, event) {
      console.log(tab, event)
    },
    // 树形结构 右侧 新增子部分 点击事件
    add(data) {
      if (data) {
        this.parentId = data.id
      } else {
        this.parentId = ''
      }
      this.isEdit = false
      this.$refs.dialog.$refs.deptForm.resetFields()
      this.showDialog = true
    },
    // 树形结构 右侧 编辑子部分 点击事件
    edit(data) {
      this.isEdit = true
      this.showDialog = true
      this.parentId = data.id
    },
    // 树形结构 右侧 删除子部分 点击事件
    async del(data) {
      // 显示删除确认消息对话框
      const delRes = await this.$confirm(
        '此操作将永久删除部门, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (delRes === 'cancel') return this.$message('您取消了删除')
      // 吊桶删除接口
      const delDepartRes = await delDepartmentAPI(data.id)
      // 根据状态值, 查看是否删除成功
      if (!delDepartRes.success) return this.$message.error(delDepartRes.message)
      // 删除成功需要给用户进行提示
      this.$message.success(delDepartRes.message)
      // 删除后需要重新获取当前页面数据
      this.getDepartmentsListFn()
    },
    // dialog 重置treedate
    resetFn() {
      this.getDepartmentsListFn()
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.departments-tree {
  margin: 12px 0px;
}
</style>
